<?php

class CryptographyProcessor_Cryptography  {
	
	const publicKeyFile = 'file://keyfiles/publicKey.pem';
	const privateKeyFile = 'file://keyfiles/privateKey.pem';
	
	/**
	* This function is used to decrypt the email of more than on subscriber
	* 
	* $subscribers - is prepared to take the array structure of the database directly
	* 
	* structure $subscribers
	* array
  	* 	'ColumnNames' => 
    * 		array
    *			0 => string 'uid' (length=3)
    *			1 => string 'datum' (length=5)
  	*	0 => 
    *		array
    *			'uid' => string '1' (length=1)
    *			'datum' => string '2012-01-27' (length=10)
	* 
	* @param array $subscribers
	* @param String $passphrase
	*/
	public static function decryptEmails ($subscribers, $passphrase)  {
	
		foreach ($subscribers as $key=>$subscriber)  {
			if (is_numeric($key))  {
				$subscribers[$key]['email'] = CryptographyProcessor_Cryptography::decryptString($subscriber['email'], CryptographyProcessor_Cryptography::privateKeyFile, $passphrase);
			}
		}
	
		return $subscribers;
	}
	
	
	public static function encryptString ($string, $file)  {
		$key = openssl_pkey_get_public($file);
		openssl_public_encrypt($string, $encrypted, $key);
		
		//In order to keep up the special chars encode the string with base64
		return base64_encode($encrypted);
	}
	
	public static function decryptString ($string, $file, $passphrase)  {
		$key = openssl_pkey_get_private(array($file, $passphrase));
		//Decode base64 encrypted data
		$string = base64_decode($string);
		openssl_private_decrypt($string, $decrypted, $key);
		return $decrypted;
	}
	
}